package com.pagepub.core.tag.validator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

public class IsExit extends FormValidateBasic {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6288622092935255085L;

	/**
	 * @param args
	 * 
	 */

	private String callfuntionaname;

	private String functionname;

	private String rightimage;

	// private String emptyelement;
	// private String pluselement;
	private String submitvalidate;

	private String rightwordcolor;;

	public String getRightwordcolor() {
		return rightwordcolor;
	}

	public void setRightwordcolor(String rightwordcolor) {
		this.rightwordcolor = rightwordcolor;
	}

	// public String getEmptyelement() {
	// return emptyelement;
	// }
	// public void setEmptyelement(String emptyelement) {
	// this.emptyelement = emptyelement;
	// }
	// public String getPluselement() {
	// return pluselement;
	// }
	// public void setPluselement(String pluselement) {
	// this.pluselement = pluselement;
	// }
	public String getRightimage() {
		return rightimage;
	}

	public void setRightimage(String rightimage) {
		this.rightimage = rightimage;
	}

	public String getFunctionname() {
		return functionname;
	}

	public void setFunctionname(String functionname) {
		this.functionname = functionname;
	}

	public String getCallfuntionaname() {
		return callfuntionaname;
	}

	public void setCallfuntionaname(String callfuntionaname) {
		this.callfuntionaname = callfuntionaname;
	}

	public StringBuffer isexit() {
		StringBuffer str = new StringBuffer();
		String emptyelement = "";
		String pluselement = "";
		// if(this.getEmptyelement().equalsIgnoreCase("true")){
		// emptyelement="emptyelement(realname);";
		// }
		// if(this.getPluselement().equalsIgnoreCase("true")){
		// pluselement="pluselement(realname);";
		// }
		if (this.submitvalidate.equalsIgnoreCase("true")) {
			emptyelement = "emptyelement(realname);";
			pluselement = "pluselement(realname);";
		}
		str.append("<script language=\"javascript\">");
		str.append("var spanname=\"\";");
		str.append("var realname=\"\";");
		str.append("var deliveryname=\"\";");
		str.append("function  " + functionname
				+ "(realvalue,tspanname,trealname,callfunvalue,iscorp){");
		str.append("spanname=tspanname;");
		// str.append("deliveryname=tdeliveryname;");
		str.append("realname=trealname;");
		// str.append("alert(callfunvalue);");
		str.append("sysDwr." + this.callfuntionaname + "(callfunvalue,iscorp,"
				+ this.callfuntionaname + "callback1);");
		str.append("}");

		str.append("function  " + this.callfuntionaname + "callback1(obj){");
		str.append(" var flag=0;");

		str.append("	var message=\"\";");
		str.append("var imgurl=\"\";");
		// str.append("alert(obj);");
		str.append("if(obj==\"1\")");
		str.append("{");
		str.append("	message=\"对不起,该\"+realname+\"已经存在\";");
		str.append("	imgurl=\"<img src=\\\"" + this.wrongimage + "\\\"/>\";");
		str.append(pluselement);
		str.append(" flag=1;");
		str.append("}");
		str.append("else if(obj==\"0\")");
		str.append("{");
		str.append("	message=\"恭喜!该\"+realname+\"可以使用\";");
		str.append("	imgurl=\"<img src=\\\"" + this.rightimage + "\\\"/>\";	");
		str
				.append("document.getElementById(spanname).innerHTML=imgurl+\"<font color=\\\""
						+ this.rightwordcolor + "\\\">\"+message+\"</font>\";");
		str.append(emptyelement);
		// if(this.submitvalidate.equalsIgnoreCase("true")){
		// str.append("dilivery(deliveryname);");
		// }
		str.append(" flag=2;");
		str.append("} else if(obj==\"3\")");
		str.append("{");
		str.append("	message=\"对不起,提交了非法字符\";		");
		str.append("	imgurl=\"<img src=\\\"" + this.wrongimage + "\\\"/>\";");
		str.append(pluselement);
		str.append(" flag=1;");
		str.append("}	");

		str.append("if(flag==1){");
		str.append(this.display());
		str.append("return false;}");
		str.append("else if(flag==0){ ");
		str.append(this.displayempty());
		str.append("return true;}");
		str.append("else return true;");
		str.append("}");
		str.append("</script>");
		return str;
	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = (JspWriter) this.pageContext.getOut();

		try {
			out.print(isexit());

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return 1;
	}

	public String getSubmitvalidate() {
		return submitvalidate;
	}

	public void setSubmitvalidate(String submitvalidate) {
		this.submitvalidate = submitvalidate;
	}
}
